package com.gxa.controller;

import com.gxa.common.LayR;
import com.gxa.common.R;
import com.gxa.param.MyTableParam;
import com.gxa.pojo.Contract;
import com.gxa.pojo.Empl;
import com.gxa.service.ContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/contract")
public class ContractController {

    @Autowired
    private ContractService contractService;

    /**
     * 渲染合同列表页面
     *
     * @return
     */
    @GetMapping("/list/page")
    public String list() {
        return "contract/contract-list";
    }

    /**
     * 渲染创建合同页面
     *
     * @return
     */
    @GetMapping("/add/page")
    public String addPage() {
        return "contract/contract-add";
    }

    /**
     * 合同的编辑页面
     *
     * @param id
     * @param model
     * @param session
     * @return
     */
    @GetMapping("/edit/page")
    public String editPage(Integer id, Model model, HttpSession session) {
        session.setAttribute("editContractId", id);
        model.addAttribute("id", id);
        return "contract/contract-edit";
    }

    /**
     * 合同的审核编辑页面
     *
     * @param id
     * @param model
     * @param session
     * @return
     */
    @GetMapping("/editExamination/page")
    public String editExaminationPage(Integer id, Model model, HttpSession session) {
        session.setAttribute("examinationContractId", id);
        model.addAttribute("id", id);
        return "contract/contract-edit-examination";
    }

    /**
     * 合同的变更编辑页面
     *
     * @param id
     * @param model
     * @param session
     * @return
     */
    @GetMapping("/editChange/page")
    public String editChangePage(Integer id, Model model, HttpSession session) {
        session.setAttribute("editChangeId", id);
        model.addAttribute("id", id);
        return "contract/contract-edit-change";
    }

    /**
     * 合同的执行编辑页面
     *
     * @param id
     * @param model
     * @param session
     * @return
     */
    @GetMapping("/editImplement/page")
    public String editImplementPage(Integer id, Model model, HttpSession session) {
        session.setAttribute("editImplementId", id);
        model.addAttribute("id", id);
        return "/contract/contract-edit-implement";
    }

    /**
     * 合同转让编辑页面
     *
     * @param id
     * @param model
     * @param session
     * @return
     */
    @GetMapping("/editExchange/page")
    public String editExchangePage(Integer id, Model model, HttpSession session) {
        session.setAttribute("editExchangeId", id);
        model.addAttribute("id", id);
        return "/contract/contract-edit-exchange";
    }

    /**
     * 合同解除编辑页面
     *
     * @param id
     * @param model
     * @param session
     * @return
     */
    @GetMapping("/editRelieve/page")
    public String editRelievePage(Integer id, Model model, HttpSession session) {
        session.setAttribute("editRelieveId", id);
        model.addAttribute("id", id);
        return "/contract/contract-edit-relieve";
    }

    /**
     * 合同审核菜单页面
     *
     * @return
     */
    @GetMapping("/examination/page")
    public String examinationPage() {
        return "contract/contract-list-examination";
    }

    /**
     * 合同管理页面
     *
     * @return
     */
    @GetMapping("/manage/page")
    public String managePage() {
        return "contract/contract-list-manage";
    }

    @GetMapping("/queryDetail/page")
    public String queryDetailPage(Integer id, Model model, HttpSession session) {
        session.setAttribute("queryContractId", id);
        model.addAttribute("id", id);
        return "contract/contract-list-detail";
    }


    /**
     * 合同列表数据
     *
     * @param param
     * @return
     */
    @PostMapping("/list/data")
    @ResponseBody
    public LayR listData(MyTableParam param) {
        return contractService.list(param);
    }

    /**
     * 添加合同
     *
     * @param contract
     * @return
     */
    @PostMapping("/add/do")
    @ResponseBody
    public R add(Contract contract) {
        return contractService.add(contract);
    }

    /**
     * 编辑页面
     *
     * @param contract
     * @param session
     * @return
     */
    @PostMapping("/edit/do")
    @ResponseBody
    public R edit(Contract contract, HttpSession session) {
        Integer editContractId = (Integer) session.getAttribute("editContractId");
        contract.setContractId(editContractId);
        return contractService.edit(contract);
    }

    /**
     * 删除合同行
     *
     * @param id
     * @return
     */
    @PostMapping("/deleteOne")
    @ResponseBody
    public R deleteOne(Integer id) {
        return contractService.deleteOne(id);

    }

    /**
     * 批量删除合同
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public R delete(@RequestParam("ids[]") String[] ids) {
        return contractService.deleteAll(ids);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @PostMapping("/detail")
    @ResponseBody
    public R detail(Integer id) {
        return contractService.findById(id);
    }

    /**
     * 详情,并显示出
     *
     * @param id
     * @return
     */
    @PostMapping("/queryDetail")
    @ResponseBody
    public LayR query(Integer id) {
        System.out.println("====================================");
        System.out.println(contractService.queryById(id));
        return contractService.queryById(id);
    }


}
